﻿function BookmarkViewModel(parent) {
    var self = this;

    self.ExpertLists = ko.observableArray([]);
    self.companyLists = ko.observableArray([]);
    self.positionLists = ko.observableArray([]);
    self.ExpertListNameExistsAlertVisible = ko.observable(false);
    self.companyListNameExistsAlertVisible = ko.observable(false);
    self.positionListNameExistsAlertVisible = ko.observable(false);
    self.newExpertList = ko.observable(new ExpertList({ }));
    self.newCompanyList = ko.observable(new CompanyList({ }));
    self.newPositionList = ko.observable(new PositionList({ }));
    self.bookmarkModel = ko.observable(new BookmarkModel({ }));
    self.profileAddedToExpertListAlert = ko.observable(false);
    self.companyAddedToCompanyListAlert = ko.observable(false);
    self.positionAddedToPositionListAlert = ko.observable(false);
    self.profileBlockedAlert = ko.observable(false);
    self.companyBlockedAlert = ko.observable(false);
    self.positionBlockedAlert = ko.observable(false);

    self.loadExpertLists = function () {
        $.ajax({
            url: "/api/ExpertList/List",
            type: 'POST',
            dataType: 'json',
            data: {},
            contentType: "application/json",
            success: function (data) {
                var expertLists = $.map(data, function (item) { return new ExpertList(item); });
                self.ExpertLists(expertLists);
            }
        });
    };
    self.loadCompanyLists = function () {
        $.ajax({
            url: "/api/CompanyList/List",
            type: 'POST',
            dataType: 'json',
            data: {},
            contentType: "application/json",
            success: function (data) {
                var companyLists = $.map(data, function (item) { return new CompanyList(item); });
                self.companyLists(companyLists);
            }
        });
    };
    self.loadPositionLists = function () {
        $.ajax({
            url: "/api/PositionList/List",
            type: 'POST',
            dataType: 'json',
            data: {},
            contentType: "application/json",
            success: function (data) {
                var positionLists = $.map(data, function (item) { return new PositionList(item); });
                self.positionLists(positionLists);
            }
        });
    };

    //SAVE Expert LIST
    self.saveExpertList = function() {
        $.ajax({
                url: "/api/ExpertList/PostExpertList",
                type: 'post',
                data: ko.toJSON(self.newExpertList()),
                contentType: 'application/json',
                statusCode: {
                    201: function () {
                        $('#CreateExpertListDialog').modal('hide');
                        self.ExpertListNameExistsAlertVisible(false);
                        self.loadExpertLists();
                    },
                    403: function () {
                        self.ExpertListNameExistsAlertVisible(true);
                    }
                },
        });
        self.newExpertList(new ExpertList({ }));
    };
    self.cancelExpertList = function() {
        $('#CreateExpertListDialog').modal('hide');
        self.ExpertListNameExistsAlertVisible(false);
        self.newExpertList(new ExpertList({ }));
    };

    //SAVE COMPANY LIST
    self.saveCompanyList = function() {
        $.ajax({
            url: "/api/CompanyList/PostCompanyList",
            type: 'post',
            data: ko.toJSON(self.newCompanyList()),
            contentType: 'application/json',
            statusCode: {
                201: function () {
                    $('#CreateCompanyListDialog').modal('hide');
                    self.companyListNameExistsAlertVisible(false);
                    self.loadCompanyLists();
                },
                403: function () {
                    self.companyListNameExistsAlertVisible(true);
                }
            },
        });
        self.newCompanyList(new CompanyList({ }));
    };
    self.cancelCompanyList = function() {
        $('#CreateCompanyListDialog').modal('hide');
        self.companyListNameExistsAlertVisible(false);
        self.newCompanyList(new CompanyList({ }));
    };

    //SAVE position LIST
    self.savePositionList = function() {
        $.ajax({
            url: "/api/PositionList/PostPositionList",
            type: 'post',
            data: ko.toJSON(self.newPositionList()),
            contentType: 'application/json',
            statusCode: {
                201: function () {
                    $('#CreatePositionListDialog').modal('hide');
                    self.positionListNameExistsAlertVisible(false);
                    self.loadPositionLists();
                },
                403: function () {
                    self.positionListNameExistsAlertVisible(true);
                }
            },
        });
        self.newPositionList(new PositionList({ }));
    };
    self.cancelPositionList = function() {
        $('#CreatePositionListDialog').modal('hide');
        self.positionListNameExistsAlertVisible(false);
        self.newPositionList(new PositionList({ }));
    };

    //ADD PROFILE TO EXPERTLIST / BLOCK LIST
    self.selectProfileLoadExpertLists = function(profile) {
        self.loadExpertLists();
        //reset bookmark model
        self.bookmarkModel(new BookmarkModel({}));
        self.bookmarkModel().ExpertProfile(profile);
    };
    self.addProfileToExpertList = function(expertList) {
        self.bookmarkModel().ExpertList(expertList);
        $.ajax({
                url: "/api/ExpertList/AddProfile",
                type: 'post',
                data: ko.toJSON(self.bookmarkModel()),
                contentType: 'application/json',
                statusCode: {
                    200: function () {
                        self.profileAddedToExpertListAlert(true);
                        setTimeout(function () { self.profileAddedToExpertListAlert(false); }, 5000);
                    }
                }
            });
    };
    self.blockProfile = function (profile) {
        self.bookmarkModel().ExpertProfile(profile);
        self.bookmarkModel().IsBlock(true);
        $.ajax({
            url: "/api/ExpertList/AddProfile",
            type: 'post',
            data: ko.toJSON(self.bookmarkModel()),
            contentType: 'application/json',
            statusCode: {
                200: function () {
                    self.profileBlockedAlert(true);
                    setTimeout(function () { self.profileBlockedAlert(false); }, 5000);
                }
            }
        });
    };

    //ADD COMPANY TO COMPANYLIST
    self.selectCompanyLoadCompanyLists = function(company) {
        self.loadCompanyLists();
        //reset bookmark model
        self.bookmarkModel(new BookmarkModel({}));
        self.bookmarkModel().CompanyProfile(company);
    };
    self.addCompanyToCompanyList = function(companyList) {
        self.bookmarkModel().CompanyList(companyList);
        $.ajax({
                url: "/api/CompanyList/AddCompany",
                type: 'post',
                data: ko.toJSON(self.bookmarkModel()),
                contentType: 'application/json',
                statusCode: {
                    200: function () {
                        self.companyAddedToCompanyListAlert(true);
                        setTimeout(function () { self.companyAddedToCompanyListAlert(false); }, 5000);
                    }
                }
            });
    };
    self.blockCompany = function (company) {
        self.bookmarkModel().CompanyProfile(company);
        self.bookmarkModel().IsBlock(true);
        $.ajax({
            url: "/api/CompanyList/AddCompany",
            type: 'post',
            data: ko.toJSON(self.bookmarkModel()),
            contentType: 'application/json',
            statusCode: {
                200: function () {
                    self.companyBlockedAlert(true);
                    setTimeout(function () { self.companyBlockedAlert(false); }, 5000);
                }
            }
        });
    };
        //ADD Position TO PositionLIST
        self.selectPositionLoadPositionLists = function (position) {
            self.loadPositionLists();
            //reset bookmark model
            self.bookmarkModel(new BookmarkModel({ }));
            self.bookmarkModel().Position(position);
        };
        self.addPositionToPositionList = function (positionList) {
            self.bookmarkModel().PositionList(positionList);
            $.ajax({
                url: "/api/PositionList/AddPosition",
                type: 'post',
                data: ko.toJSON(self.bookmarkModel()),
                contentType: 'application/json',
                statusCode: {
                    200: function () {
                        self.positionAddedToPositionListAlert(true);
                        setTimeout(function () { self.positionAddedToPositionListAlert(false); }, 5000);
                    }
                }
            });
        };
        self.blockPosition = function (position) {
            self.bookmarkModel().Position(position);
            self.bookmarkModel().IsBlock(true);
            $.ajax({
                url: "/api/PositionList/AddPosition",
                type: 'post',
                data: ko.toJSON(self.bookmarkModel()),
                contentType: 'application/json',
                statusCode: {
                    200: function () {
                        self.positionBlockedAlert(true);
                        setTimeout(function () { self.positionBlockedAlert(false); }, 5000);
                    }
                }
            });
        };
};